App({

  globalData:{
    avatarUrl:null,
    nickName:null,
    token:""
  },
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function () {
    var avatarUrl = wx.getStorageSync('avatarUrl');
    var nickName = wx.getStorageSync('nickName');
    var token = wx.getStorageSync('token');
    console.log(nickName);
    console.log(avatarUrl);
    console.log(token);
    if(nickName){
      this.globalData.nickName = nickName;
    }
    if(avatarUrl){
      this.globalData.avatarUrl = avatarUrl;
    }
    if(token){
      this.globalData.token = token;
    }
    
  },

  // 初始化头像
  initavatarUrl:function(res){
    this.globalData.avatarUrl = res;
    wx.setStorageSync("avatarUrl", res);
  },

  // 初始化昵称
  initnickName:function(res){
    this.globalData.nickName = res;
    wx.setStorageSync("nickName", res);
  },


  initToken:function(res){
    console.log(res)
    this.globalData.token = res;
    wx.setStorageSync("token", res);
  },


  // 删除本地或者内存中的数据
  delUserInfo:function(){
    this.globalData.avatarUrl = null;
    wx.removeStorageSync('avatarUrl');
    this.globalData.nickName = null;
    wx.removeStorageSync('nickName');
    this.globalData.token = "";
    // wx.removeStorageSync('token');
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function (options) {
    var token = wx.getStorageSync('token');
    if(token){
      this.globalData.token = token;
    }
  },

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function () {
    
  },

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function (msg) {
    
  }
})
